#include "IniFile.h"

IniFile::IniFile(void)
{
}

IniFile::~IniFile(void)
{
	map<tstring,IniSection*>::iterator it;
	for ( it = m_IniSectionMap.begin();
		it != m_IniSectionMap.end();
		it++ )
	{
		delete (*it).second;
	}

	
}


void IniFile::AddIniSection(IniSection* iniSection)
{
	m_IniSectionMap[iniSection->GetSectionName()]=iniSection;
}

const tstring& IniFile::GetValue(tstring sectionName, tstring keyName) const
{
	static tstring nullReturnValue(_T(""));
	map<tstring,IniSection*>::const_iterator it = m_IniSectionMap.find(sectionName);
	if ( it != m_IniSectionMap.end()){
		IniSection* section = (*it).second;
		return section->GetValue(keyName);
	}else
		return nullReturnValue;
}

void IniFile::SetValue(tstring sectionName, tstring keyName,tstring value)
{
	map<tstring,IniSection*>::const_iterator it = m_IniSectionMap.find(sectionName);
	IniSection* section;
	if ( it == m_IniSectionMap.end() )
	{
		section = new IniSection(sectionName);
		AddIniSection(section);
	}else{
		section = (*it).second;
	}
	section->AddKeyValue(keyName,value);
}

bool IniFile::HasValue( tstring sectionName,  tstring keyName) const
{
	map<tstring,IniSection*>::const_iterator it = m_IniSectionMap.find(sectionName);
	if (it !=  m_IniSectionMap.end()){
		IniSection* section = (*it).second;
		return section->HasValue(keyName);
	}else
		return false;
}